package com.hot12345.server.DBConnection;

import java.sql.SQLException;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.logicalcobwebs.proxool.ProxoolDataSource;

import com.ibatis.sqlmap.engine.datasource.DataSourceFactory;

public class ProxoolDataSourceFactory implements DataSourceFactory {

	private static final Log log = LogFactory.getLog(ProxoolDataSourceFactory.class);
	 /** 
     * 使用ProxoolDataSource数据源 
     */  
    private ProxoolDataSource dataSource;
    //不能静态化,因为不同配置文件读取数据源不一样，传入的别名不一样 
    
    public DataSource getDataSource() {  
    	
        return dataSource;  
    }  
  
    /**                                                     
     * 此方法中实现对ProxoolDataSource类中相关属性的设置 
     */
    @SuppressWarnings("unchecked")
	public void initialize(Map map) {          	
        dataSource = new ProxoolDataSource(); 
        dataSource.setDriver("org.logicalcobwebs.proxool.ProxoolDriver");
        //dataSource.setDriverUrl("jdbc:mysql://192.169.100.127/tianyaclub3?useUnicode=true&characterEncoding=utf8");
        dataSource.setAlias((String)map.get("alias"));   
        
//        if("1".equals(ReadProperties.readValueBySystem("debug"))){//测试模式 
        	 dataSource.setDriver((String)map.get("driver"));  
             dataSource.setDriverUrl((String)map.get("driverUrl"));  
             dataSource.setUser((String)map.get("user"));  
             dataSource.setPassword((String)map.get("password"));  
//           dataSource.setAlias("alias"); 
             dataSource.setHouseKeepingTestSql((String)map.get("houseKeepingTestSql"));
             if((String)map.get("houseKeepingSleepTime")!=null && !"".equals(map.get("houseKeepingSleepTime").toString().trim())){
         		 dataSource.setHouseKeepingSleepTime(Integer.parseInt((String)map.get("houseKeepingSleepTime")));
         	}  
                                          
             try {
             	if((String)map.get("loginTimeout")!=null && !"".equals(map.get("loginTimeout").toString().trim())){
            			dataSource.setLoginTimeout(Integer.parseInt((String)map.get("loginTimeout")));
             	}
     		} catch (SQLException e) {
     			// TODO Auto-generated catch block
     			log.error("SQLException",e);
     		}
     		if((String)map.get("loginTimeout")!=null && !"".equals(map.get("maximumActiveTime").toString().trim())){
        			dataSource.setMaximumActiveTime(Long.parseLong((String)map.get("maximumActiveTime")));
         	}
     		if((String)map.get("maximumConnectionCount")!=null && !"".equals(map.get("maximumConnectionCount").toString().trim())){
        			dataSource.setMaximumConnectionCount(Integer.parseInt((String)map.get("maximumConnectionCount")));
         	}
     		if((String)map.get("maximumConnectionLifetime")!=null && !"".equals(map.get("maximumConnectionLifetime").toString().trim())){
        			dataSource.setMaximumConnectionLifetime(Integer.parseInt((String)map.get("maximumConnectionLifetime")));
        			
     		}
     		if((String)map.get("minimumConnectionCount")!=null && !"".equals(map.get("minimumConnectionCount").toString().trim())){
        			dataSource.setMinimumConnectionCount(Integer.parseInt((String)map.get("minimumConnectionCount")));
         	}
     		if((String)map.get("testAfterUse")!=null && !"".equals(map.get("testAfterUse").toString().trim())){
        			dataSource.setTestAfterUse(Boolean.parseBoolean((String)map.get("testAfterUse")));
         	}
     		if((String)map.get("testBeforeUse")!=null && !"".equals(map.get("testBeforeUse").toString().trim())){
        			dataSource.setTestBeforeUse(Boolean.parseBoolean((String)map.get("testBeforeUse")));
         	}
     		if((String)map.get("trace")!=null && !"".equals(map.get("trace").toString().trim())){
        			dataSource.setTrace(Boolean.parseBoolean((String)map.get("trace")));
         	}
     		if((String)map.get("prototypeCount")!=null && !"".equals(map.get("prototypeCount").toString().trim())){
        			dataSource.setPrototypeCount(Integer.parseInt((String)map.get("prototypeCount")));
        			System.out.println("prototypeCount : " + Integer.parseInt((String)map.get("prototypeCount")));
         	}
     		if((String)map.get("maximumActiveTime")!=null && !"".equals(map.get("maximumActiveTime").toString().trim())){
        			dataSource.setMaximumActiveTime(Integer.parseInt((String)map.get("maximumActiveTime")));
         	}
     		if(map.get("simultaneousBuildThrottle")!=null && !"".equals(map.get("simultaneousBuildThrottle").toString().trim())){
     			dataSource.setSimultaneousBuildThrottle(Integer.parseInt(map.get("simultaneousBuildThrottle").toString()));
     		}
     		if(map.get("statisticsLogLevel")!=null && !"".equals(map.get("statisticsLogLevel").toString().trim())){
     			dataSource.setStatisticsLogLevel(map.get("statisticsLogLevel").toString());
     		}
     		if(map.get("statistics")!=null && !"".equals(map.get("statistics").toString().trim())){
     			dataSource.setStatistics(map.get("statistics").toString());
     		}
     		if(map.get("recentlyStartedThreshold")!=null && !"".equals(map.get("recentlyStartedThreshold").toString().trim())){
     			dataSource.setRecentlyStartedThreshold(Integer.parseInt(map.get("recentlyStartedThreshold").toString()));
     		}
     		if(map.get("overloadWithoutRefusalLifetime")!=null && !"".equals(map.get("overloadWithoutRefusalLifetime").toString().trim())){
     			dataSource.setOverloadWithoutRefusalLifetime(Integer.parseInt(map.get("overloadWithoutRefusalLifetime").toString()));
     		}
//        }

    }
     
 
}
